<script>
import { mapGetters, mapMutations } from 'vuex'
import { getAutoCompanyCert } from '@/common/api/user'
export default {
  computed: {
    ...mapGetters({
      userInfo: 'userInfo',
      userId: 'userId',
	  token: 'token',
	  materialpromptShow: 'materialpromptShow'
    })
  },
  created() {
    this.setBaseUrl('')
  },
  onLaunch() {
    this.watchNetWork()
    this.getProvider()
    this.setStatisticsCalendarShowInx(null)
    this.checkIdentity()
  },
  onShow() {
  	if(this.token){
  		console.log('1111111')
  		this.setMaterialpromptShow(true)
  	}
  },
  methods: {
    // 监听网络变化
    watchNetWork() {
      uni.getNetworkType({
        complete: res => {
          if (res.networkType === 'none') {
            this.toast('网络异常，请检查网络连接')
          }
        }
      })
      uni.onNetworkStatusChange(res => {
        if (res.isConnected) {
          const routes = getCurrentPages() // 获取当前打开过的页面路由数组
          const curRoute = routes[routes.length - 1].route // 获取当前页面路由
          const curParam = routes[routes.length - 1].options // 获取路由参数
          let param = ''
          for (const key in curParam) {
            param += '&' + key + '=' + curParam[key]
          }
          this.jump('/' + curRoute + '?' + param, 'redirectTo')
        } else {
          this.toast('网络异常，请检查网络连接')
        }
      })
    },
    getProvider() {
      uni.getProvider({
        service: 'oauth',
        success: (res) => {
          let oauth = res.provider[0]
          /* #ifdef APP-PLUS*/
          oauth = 'app'
          /* #endif*/
          this.setOauth(oauth)
        }
      })
    },
    // 身份验证
    async checkIdentity() {
      if (this.userInfo && this.userInfo.certTypeLabel === 'personal') {
        // 如果用旅行社账号登录则返回个人身份
        this.jump('/passport/cert/cert?type=1')
      } else {
        // 否则返回车企身份
        await this.getAutoCompanyCert()
      }
    },
    // 检测用户是否认证
    async getAutoCompanyCert() {
      if (this.userInfo) {
        if ((this.userInfo.certType.indexOf('20') != -1 || this.userInfo.certType.indexOf('21') != -1) && this.userInfo.certTypeLabel === 'autoCompany') {
          const data = {
            userId: this.userId
          }
          // 查看车企认证资料
          const [err, res] = await getAutoCompanyCert(this.userId, data)
          const certStatus = res.data.data.certStatus
          if (certStatus != 1) {
            this.jump('/passport/cert/cert')
          }
        }
      }
    },
    ...mapMutations({
      setOauth: 'user/setOauth',
      setStatisticsCalendarShowInx: 'status/setStatisticsCalendarShowInx',
      setBaseUrl: 'user/setBaseUrl',
	  setMaterialpromptShow:'vehicle/setMaterialpromptShow'
    })
  }
}
</script>

<style lang="scss">
	/*每个页面公共css */
  @import "./common/global.scss";

  /*#ifndef APP-NVUE*/
  @import "./common/iconfont.css";
  /*#endif*/
  @import 'uni_modules/uview-ui/index.scss'
</style>



